
package com.sf.datastructure.imageUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

/**
 * system constant class
 * @author huangcunyin
 *
 */
@Component
public class SysConstants implements ResourceLoaderAware,
		InitializingBean{
	
	private static final Logger logger = LoggerFactory.getLogger(SysConstants.class);
	
	private static Properties sysProperties = new Properties();
	
	private ResourceLoader resourceLoader;
	
	private String[] locations;
	
	/**
	 * 附件上传的根路径
	 */
	@Value("${ATTACHMENT_SAVE_ROOT_PATH}")
	private String basePath;
	/**
	 * PDF转SWF工具地址
	 */
	@Value("${PDF2SWF_PATH}")
	private String pdf2swfPath;
	/**
	 * WEB工程访问地址
	 */
	@Value("${ATTACHMENT_VIEW_PATH}")
	private String viewPath;
	
	/**
	 * openoffice目录
	 */
	@Value("${OPENOFFICE_PATH}")
	private String openofficePath;
	
	/**
	 * XPDF路径
	 */
	@Value("${XPDF_PATH}")
	private String xpdfPath;
	
	/**达人个人设置私信管理员工号*/
	@Value("${MASTER_MANAGE_EMP_NUMBER}")
	private String masterManageEmpNumber;
	
	
	/** 丰声消息中心发送地址 */
	@Value("${SFIM_SEND_MESSAGE}")
	private String sfimSendMessage;
	
	public SysConstants() {
		//throw new UnsupportedOperationException();
	}
	
	/**  */
	public String getBASE_PATH() {
		return basePath;
	}
	/**  */
	public void setBASE_PATH(String bASE_PATH) {
		basePath = bASE_PATH;
	}
	/**  */
	public String getPDF2SWF_PATH() {
		return pdf2swfPath;
	}
	/**  */
	public void setPDF2SWF_PATH(String pDF2SWF_PATH) {
		pdf2swfPath = pDF2SWF_PATH;
	}
	/**  */
	public String getVIEW_PATH() {
		return viewPath;
	}
	/**  */
	public void setVIEW_PATH(String vIEW_PATH) {
		viewPath = vIEW_PATH;
	}

	/**  */
	public String getOPENOFFICE_PATH() {
		return openofficePath;
	}
	/**  */
	public void setOPENOFFICE_PATH(String oPENOFFICE_PATH) {
		openofficePath = oPENOFFICE_PATH;
	}
	/**  */
	public String getXPDF_PATH() {
		return xpdfPath;
	}
	/**  */
	public void setXPDF_PATH(String xPDF_PATH) {
		xpdfPath = xPDF_PATH;
	}
	
	public String getMasterManageEmpNumber() {
		return masterManageEmpNumber;
	}

	public void setMasterManageEmpNumber(String masterManageEmpNumber) {
		this.masterManageEmpNumber = masterManageEmpNumber;
	}
	
	


	public String getSfimSendMessage() {
		return sfimSendMessage;
	}

	public void setSfimSendMessage(String sfimSendMessage) {
		this.sfimSendMessage = sfimSendMessage;
	}

	/**
	 * 获取附件查看路径前缀
	 * @return
	 */
	public static String getAttachmentViewPath() {
		String viewPath = sysProperties.getProperty("ATTACHMENT_VIEW_PATH");
		if(!viewPath.endsWith("/")){
			viewPath += "/";
		}
		return viewPath;
	}
	
	/**
	 * 依据key获取配置的值
	 * @param key
	 * @return
	 */
	public static String getConfigValue(String key){
		return sysProperties.getProperty(key);
	}
	
	/**
	 * 设置系统配置文件地址
	 * @param locations
	 */
	public void setLocations(String[] locations){
		this.locations = locations;
	}

	@Override
	public void setResourceLoader(ResourceLoader resourceLoader) {
		this.resourceLoader = resourceLoader;
	}
	
	@Override
	public void afterPropertiesSet() throws Exception {
		if(locations != null){
			for (String location : locations) {
				Resource re = resourceLoader.getResource(location);
				logger.info("加载系统资源文件:"+re.getFilename());
				InputStream input = null;
				try {
					input = re.getInputStream();
					SysConstants.sysProperties.load(input);
				} catch (IOException e) {
					logger.error("加载 系统配置信息出错",e);
				}finally{
					if(input != null){
						try {
							input.close();
						} catch (IOException e) {
							logger.info("关闭失败",e);
						}
					}
				}
				
			}
		}
	}
}
